<?php
class vmarket
{

	public static function loadurl($url,$post)
	{
		$timeout=200;
		$ch = curl_init(); 
		curl_setopt($ch, CURLOPT_URL, $url); 
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/6.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.7) Gecko/20050414 Firefox/1.0.3"); 
		curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); 
		curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); 
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}
	public static function GetContents($string,$start,$end)
	{
		$temp=explode($start,$string);
		$Result=explode($end,$temp[1]);
		return $Result[0];
	}
	public static function logoutVmarket()
	{
		$urlLogout="https://www.v-market.org/logout";
		$logout=self::loadurl($urlLogout, "0");
		return TRUE;
	}
	public static function loginVmarket()
	{
		$logout=self::logoutVmarket();
		$urlLogin="https://www.v-market.org/login";
		$post="c_login=freshcv2shop&c_pass=123abc@@@&login_button=Log In";
		$login=self::loadurl($urlLogin, $post);
		return $login;
	}
	public static function checkBalanceVmarket()
	{
		$urlCheckBalance="http://174.120.169.218/~only/tuyen/tuyen.php";
		$post="pass=nguyentuyen!!!@@@###123&action=getbalance";
		$checkBalance=self::loadurl($urlCheckBalance, $post);
		$start='<b id="balance" >$';
		$end="</b><br />";
		$getBalance=self::GetContents($checkBalance, $start, $end);
		$money=intval($getBalance);
		return $money;
	}
	public static function findCountryVmarket()
	{
		$url="http://174.120.169.218/~only/tuyen/tuyen.php";
		$post="pass=nguyentuyen!!!@@@###123&action=findcountry";
		$getCountry=self::loadurl($url, $post);
		$start='<option value="all">All</option>';
		$end="</select>";
		$country=self::GetContents($getCountry, $start, $end);
		$delimiter='</option>';
			$arryCountry=explode($delimiter,$country);
			$totalCountry=count($arryCountry)-1;
			$arrayReturn=NULL;
			for($i=0;$i<$totalCountry;$i++)
			{
				$line=$arryCountry[$i];
				$start='<option value="';
				$end='"  >';
				$country_code=self::GetContents($line, $start, $end);
				$country_code=strtolower($country_code);
				$arrayReturn[]=$country_code;
			}
			return $arrayReturn;
		self::logoutVmarket();
	}
	public static function getStateVmartket($country)
	{
		self::loginVmarket();
		$urlState="https://www.v-market.org/cards/getstates/$country";
		$getState=self::loadurl($urlState, "0");
		$start='<option value="all" id="choseCountry">All</option>';
		$arr=explode($start,$getState);
		$state=$arr[1];
		return $state;
		
	}
	public static function getTypeVmarket()
	{
		$type='<option value="" >All</option>
			<option value="4"  >VISA</option>
			<option value="5"  >Mastercard</option>
			<option value="6"  >Discovery</option>
			<option value="3"  >AmEx</option>';
		return $type;
		
	}
	public static function searchCcVmarket($country,$state,$bin,$type,$dob,$ssn,$price)
	{
		
		if($bin==0)
		{
			$bin="";
			if(!empty($type))
			{
				$bin=$type;
			}
		}
		
	if(strlen($state)==0)
		{
			$state="all";
		}
		if(strlen($country)<1)
		{
			return 0;//erro
		}
		else
		{
			$post="country=$country&state=$state&bin=$bin&filter_button=Search&city=all&zip=&type=$type";
			if($ssn=="YES")
			{
				$post=$post."&with_ssn=on";
			}
			if($dob=="YES")
			{
				$post=$post."&with_dob=on";
			}
		//	$urlSearch="https://www.v-market.org/cards";
			//$login=self::loginVmarket();
			$postLogin="c_login=freshcv2shop&c_pass=123abc@@@&login_button=Log In";
			$urlSearch="http://174.120.169.218/~only/tuyen/tuyen.php";
			$postLogin=urlencode($postLogin);
			$post=urlencode($post);
			$post="pass=nguyentuyen!!!@@@###123&action=searchcc&post=$post";
			$search=self::loadurl($urlSearch, $post);
		
			if(stristr($search,"No one elements found by your request")==true)
			{
				return 0; // not found
			}
			else
			{
				$start='<table class="main_table"  cellpadding="0" cellspacing="0">';
				$end='</table>';
				$arrExploit=self::GetContents($search, $start, $end);
				
				$string1='<tr class="alt1" onmouseover="this.className=';
				$string2="'focus';";
				$string3='" onmouseout="this.className=';
				$string4="'alt1';";
				$string5='">';
				
				$delimiter=$string1.$string2.$string3.$string4.$string5;
				$ccArr=explode($delimiter,$arrExploit);
				$ArrReturn=array();
				for($i=1;$i<count($ccArr);$i++)
				{
					$id=$i-1;
					$variable=$ccArr[$i];
					$start='<input type="checkbox" name="';
					$end='">';
					$ccId=self::GetContents($variable, $start, $end);
					$line='<td class="first" align="center" id="'.$ccId.'" >';
					$search=array('<td align="center">','<td>',"$line","$ccId",'&nbsp;','<img src="/img/flags/','.gif"');
					$replace=array("","","","","","","");
					$ccFormat=str_replace($search, $replace, $variable);
					$arrCc=explode("</td>",$ccFormat);
					$bin=rtrim(trim($arrCc[0]));
					$ex=rtrim(trim($arrCc[1]));
					$exM=substr($ex,0,2);
					$exY="20".substr($ex,3,2);
					$fName=rtrim(trim($arrCc[2]));
					$city=rtrim(trim($arrCc[3]));
					$state=rtrim(trim($arrCc[4]));
					$countryTemp=rtrim(trim($arrCc[5]));
					$country=substr($countryTemp,0,2);
					$zipcode=rtrim(trim($arrCc[6]));
					$dob=rtrim(trim($arrCc[7]));
					$ssn=rtrim(trim($arrCc[8]));
					$ArrReturn["$id"]['bin']=rtrim(trim($bin));
					$ArrReturn["$id"]['name']=rtrim(trim($fName));
					$ArrReturn["$id"]['exM']=rtrim(trim($exM));
					$ArrReturn["$id"]['exY']=rtrim(trim($exY));
					$ArrReturn["$id"]['city']=rtrim(trim($city));
					$ArrReturn["$id"]['state']=rtrim(trim($state));
					$ArrReturn["$id"]['country']=rtrim(trim($country));
					$ArrReturn["$id"]['zip']=rtrim(trim($zipcode));
					$ArrReturn["$id"]['price']=rtrim(trim($price));
					$ArrReturn["$id"]['ccid']=rtrim(trim($ccId));
							
				}
				return $ArrReturn;
				
			}
			
			
		}
		
	}
	public static function searchDumpVmarket($country,$state,$bin,$type,$dob,$ssn,$price)
	{
	if($bin==0)
		{
			$bin="";
		}
	if(strlen($state)==0)
		{
			$state="all";
		}
		if(strlen($country)<1)
		{
			return 0;//erro
		}
		else
		{
			$post="country=$country&state=$state&bin=$bin&filter_button=Search&city=all&zip=&type=$type";
			if($ssn=="YES")
			{
				$post=$post."&with_ssn=on";
			}
			if($dob=="YES")
			{
				$post=$post."&with_dob=on";
			}
			$urlSearch="https://www.v-market.org/cards";
			$login=self::loginVmarket();
			$search=self::loadurl($urlSearch, $post);
			if(stristr($search,"No one elements found by your request")==true)
			{
				return 0; // not found
			}
			else
			{
				$start='<table class="main_table"  cellpadding="0" cellspacing="0">';
				$end='</table>';
				$arrExploit=self::GetContents($search, $start, $end);
				
				$string1='<tr class="alt1" onmouseover="this.className=';
				$string2="'focus';";
				$string3='" onmouseout="this.className=';
				$string4="'alt1';";
				$string5='">';
				
				$delimiter=$string1.$string2.$string3.$string4.$string5;
				$ccArr=explode($delimiter,$arrExploit);
				$ArrReturn=array();
				for($i=1;$i<count($ccArr);$i++)
				{
					$id=$i-1;
					$variable=$ccArr[$i];
					$start='<input type="checkbox" name="';
					$end='">';
					$ccId=self::GetContents($variable, $start, $end);
					$line='<td class="first" align="center" id="'.$ccId.'" >';
					$search=array('<td align="center">','<td>',"$line","$ccId",'&nbsp;','<img src="/img/flags/','.gif"');
					$replace=array("","","","","","","");
					$ccFormat=str_replace($search, $replace, $variable);
					$arrCc=explode("</td>",$ccFormat);
					$bin=rtrim(trim($arrCc[0]));
					$ex=rtrim(trim($arrCc[1]));
					$exM=substr($ex,0,2);
					$exY="20".substr($ex,3,2);
					$fName=rtrim(trim($arrCc[2]));
					$city=rtrim(trim($arrCc[3]));
					$state=rtrim(trim($arrCc[4]));
					$countryTemp=rtrim(trim($arrCc[5]));
					$country=substr($countryTemp,0,2);
					$zipcode=rtrim(trim($arrCc[6]));
					$dob=rtrim(trim($arrCc[7]));
					$ssn=rtrim(trim($arrCc[8]));
					$ArrReturn["$id"]['bin']=rtrim(trim($bin));
					$ArrReturn["$id"]['name']=rtrim(trim($fName));
					$ArrReturn["$id"]['exM']=rtrim(trim($exM));
					$ArrReturn["$id"]['exY']=rtrim(trim($exY));
					$ArrReturn["$id"]['city']=rtrim(trim($city));
					$ArrReturn["$id"]['state']=rtrim(trim($state));
					$ArrReturn["$id"]['country']=rtrim(trim($country));
					$ArrReturn["$id"]['zip']=rtrim(trim($zipcode));
					$ArrReturn["$id"]['price']=rtrim(trim($price));
					$ArrReturn["$id"]['ccid']=rtrim(trim($ccId));
							
				}
				return $ArrReturn;
				
			}
			
		}
		
	}
	public static function emptyCart()
	{
		$urlCart="https://www.v-market.org/cart";
		$loadCart=self::loadurl($urlCart, "0");
		$string1='<tr class="alt1" onmouseover="this.className=';
		$string2="'focus';";
		$string3='" onmouseout="this.className=';
		$string4="'alt1';";
		$string5='">';
		$delimiter=$string1.$string2.$string3.$string4.$string5;
		$arrContents=explode($delimiter,$loadCart);
		$start='<input type="checkbox" name="';
		$end='">';
		$post="";
		for($i=1;$i<count($arrContents);$i++)
		{
			$veriable=$arrContents[$i];
			$ccId=self::GetContents($veriable, $start, $end);
			$post=$post."$ccId=$ccId&";
		}
		$post=$post."del_but=Delete";
		$url="https://www.v-market.org/client/menagecart";
		$emptyCart=self::loadurl($url, $post);
		if(stristr($emptyCart,"0 item for $0")==true)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
		
	}
	public static function addToCartVmarket($ccid)
	{
		$post=null;
		foreach($ccid as $cc)
		{
			$post=$post."$cc=$cc&";
		}
		$post=$post."page=1&add_but=Add to cart";
		$post=urlencode($post);
		$urlAdd2Cart="http://174.120.169.218/~only/tuyen/tuyen.php";
		$postAdd2Cart="pass=nguyentuyen!!!@@@###123&action=add2cart&add2cart=$post";
		$addToCart=self::loadurl($urlAdd2Cart, $postAdd2Cart);
		return TRUE;
	}
	public static function checkOutVmarket($ccid,$quality,$Country,$price,$userID,$orderId)
	{
		
		$add2Cart=self::addToCartVmarket($ccid);
		$urlCart="http://174.120.169.218/~only/tuyen/tuyen.php";
		$postCheckout="pass=nguyentuyen!!!@@@###123&action=loadcart";
		$loadCart=self::loadurl($urlCart,$postCheckout);
		$string1='<tr class="alt1" onmouseover="this.className=';
		$string2="'focus';";
		$string3='" onmouseout="this.className=';
		$string4="'alt1';";
		$string5='">';
		$delimiter=$string1.$string2.$string3.$string4.$string5;
		$arrContents=explode($delimiter,$loadCart);
		$start='<input type="checkbox" name="';
		$end='">';
		$post="";
		for($i=1;$i<count($arrContents);$i++)
		{
			$veriable=$arrContents[$i];
			$ccId=self::GetContents($veriable, $start, $end);
			$post=$post."$ccId=$ccId&";
		}
		$post=$post."buy_but=Check And Buy Valid *";
		$post=urlencode($post);
		$url="http://174.120.169.218/~only/tuyen/tuyen.php";
		$postCheckoutCart="action=checkout&pass=nguyentuyen!!!@@@###123&postcheckout=$post";
		$buy=self::loadurl($url, $postCheckoutCart);
		$start='<textarea style="width: 100%" rows="25">';
		$end='</textarea>';
		$ccLine1=self::GetContents($buy, $start, $end);
		if(stristr($ccLine1,"APPROVAL")==true)
		{
			$search=array("Checker error","DECLINE");
			$replace=array("APPROVAL","APPROVAL");
			$ccLine=str_replace($search, $replace, $ccLine1);
			$delimit='APPROVAL';
			$arrCcLine=explode($delimit,$ccLine);
			$refund=0;
			for($i=0;$i<$quality;$i++)
			{
				$ccLineX=$arrCcLine[$i];
				if(stristr($ccLineX,"*************")==true)
				{
					$refund=$refund+1;
				}
				else 
				{
					$ccArr=explode("|",$ccLineX);
					$ccNumber="0";
					$ccv="0";
					$ex="0";
					$exM="0";
					$exY="0";
					for($id=0;$id<count($ccArr);$id++)
					{
						$variable=rtrim(trim($ccArr[$id]));
						if((strlen($variable)>14)&&(ctype_digit($variable)==true))
						{
							$ccNumber=$variable;
							$idCcv=$id+1;
							$ccv=rtrim(trim($ccArr[$idCcv]));
							$idEx=$id+3;
							$ex=rtrim(trim($ccArr[$idEx]));
							$exM=substr($ex,0,2);
							$exY="20".substr($ex,3,2);
						
						}
					}
					$search=array($ccNumber,$ex,$ccv);
					$replace=array("","","");
					$info=str_replace($search, $replace, $ccLineX);
					$ccNumberAdd=hash::hashccv($ccNumber, $exM, $exY, $ccv);
					if(substr($ccNumber, 0,1)==4)
					{
						$Typecc="VISA";
					}
					else if(substr($ccNumber, 0,1)==5)
					{
						$Typecc="MASTER";
					}
					else if(substr($ccNumber, 0,1)==3)
					{
						$Typecc="AMEX";
					}
					else if(substr($ccNumber, 0,1)==6)
					{
						$Typecc="DIS";
					}
					else 
					{
						$Typecc="unknow";
					}
					$InsertCC=database::Insert('ccinfo', array(
					'TypeCC' => $Typecc,
					'CCnumber' => $ccNumberAdd,
					'ExMonth' => $exM,
					'ExYear' => $exY,
					'CCV2' => $ccv,
					'Others' => $info,
					'site' => 'vmarket',
					'country' => $Country,
					'pricesell' => $price,
					'userid' => $userID,
					'orderid' => $orderId,
					'status' => 'valid'
						));
					
					}
				}
				
				return $refund;
				
			}
			else 
			{
				return 100; // refund
			}
		
		
		
		
	}
	public static function showVmartketCountry()
	{
		$getCountry=database::LimitQuery("country",array(
		'select' => 'country_code,country_name',
		'condition' => array(
		'agent_class' => 'vmarket'
		)
		));
		$return=NULL;
		foreach($getCountry as $country)
		{
			$country_code=$country['country_code'];
			$country_name=$country['country_name']." - (".$country_code.")";
			$return="<option value='".$country_code."'></option>".$return;
		}
		return $return;
	}

}

?>